import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login";
import Home from "../views/Home";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    /*重定向至main子路由*/
    redirect: '/main',
    children: [
      {
        path: '/main',
        name: 'Main',
        meta: {
          title: "首页"
        },
        component: () => import('@/views/Main')
      },
      {
        path: '/sys/users',
        name: 'sys:user',
        meta: {
          title: "用户管理",
          icon: "el-icon-s-custom"
        },
        component: () => import('@/views/sys/User')
      },
      {
        path: '/sys/audio',
        name: 'sys:audio',
        meta: {
          title: "客家语音管理",
          icon: "el-icon-rank"
        },
        component: () => import('@/views/sys/Audio')
      },
      {
        path: '/sys/thesis',
        name: 'sys:thesis',
        meta: {
          title: "论文管理",
          icon: "el-icon-notebook-2"
        },
        component: () => import('@/views/sys/Thesis')
      },
      {
        path: '/sys/dicts',
        name: 'sys:dict',
        meta: {
          title: "数字字典",
          icon: "el-icon-s-order"
        },
        component: () => import('@/views/sys/Dict')
      },
      {
        path: '/userCenter',
        name: 'UserCenter',
        meta: {
          title: "个人中心",
          icon: null
        },
        component: () => import('@/views/UserCenter')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//路由到页面之前,做一些判断或者加载数据
router.beforeEach((to,from,next) => {
  let token = localStorage.getItem("token")
  if (to.path === '/login'){
    next()
    /*判断是否包含token*/
  }else if (!token){
    next({path: '/login'})
    //判断是否获取过路由, 没有路由才需要获取
  }
  next()
})
/*防止连续点击标签页时出现错误信息*/
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
